![]() |
Java Database Programming with JDBC
by Pratik Patel Coriolis, The Coriolis Group ISBN: 1576100561 Pub Date: 10/01/96 |
Previous | Table of Contents | Next |
However, you dont have to wait for this updated software to be released. You can grab the JDBC API classes from the accompanying CD-ROM or from the JavaSoft Web site at http://splash.java.com/jdbc. As I was writing this chapter, the classes were stored in a file named jdbc.100.tar.Z. By the time you read this chapter, however, the file name may be slightly different. Once you have your software, simply follow these easy instructions to install the API classes in the proper place on your computers hard disk. The method shown here allows you to compile and run Java applications and applets (using the Appletviewer) that use the JDBC:
Tip: Save the API documentation.
The only item left from the JDBC package you downloaded is the API documentation, which is in the jdbc\html directory that was created when you untarred the downloaded file. You may want to save that somewhere for reference. You can view the file using a Web browser.
I must stress that you should make sure that you have the CLASSPATH set properly. The package will be called in the following way in your Java program:
import java.sql.*
You need to point the CLASSPATH at the parent of the java directory you copied in Step 2, which is why we set the CLASSPATH in Step 3. The package is contained in the java/sql/ folder, which is exactly as it should be according to the calling code snippet above.
Now that weve installed the JDBC classes, lets cover how you load a JDBC driver. Note that the java.sql.* must be imported into your Java program if you want to use a JDBC driver. These JDBC base classes contain the necessary elements for properly instantiating JDBC drivers, and they serve as the middleman between you and the low-level code in the JDBC driver. The JDBC API provides you with an easy-to-use interface for interacting with data sources, independent of the driver you are using. The following sections cover three different ways to tell the JDBCs DriverManager to load a JDBC driver.
When you want to identify a list of drivers that can be loaded with the DriverManager, you can set the sql.drivers system property. Because this is a system property, it can be set at the command line using the -D option:
java -Dsql.drivers=imaginary.sql.iMsqlDriver classname
If there is more than one driver to include, just separate them using colons. If you do include more than one driver in this list, the DriverManager will look at each driver once the connection is created and decide which one matches the JDBC URL supplied in the Connection class instantiation. (Ill provide more detail on the JDBC URL and the Connection class later on.) The first driver specified in the URL that is a successful candidate for establishing the connection will be used.
You can explicitly load a driver using the standard Class.forName method. This technique is a more direct way of instantiating the driver class that you want to use in the Java program. To load the mSQL JDBC driver, insert this line into your code:
Class.forName("imaginary.sql.iMsqlDriver");
This method first tries to load the imaginary/sql/iMsqlDriver from the local CLASSPATH. It then tries to load the driver using the same class loader as the Java programthe applet class loader, which is stored on the network.
Another approach is what I call the quick and dirty way of loading a JDBC driver. In this case, you simply instantiate the drivers class. Of course, I dont advise you to take this route because the driver may not properly register with the JDBC DriverManager. The code for this technique, however, is quite simple and worth mentioning:
new imaginary.sql.iMsqlDriver;
Again, if this is in the applet context, this code will first try to find this driver in the local CLASSPATH, then it will try to load it from the network.
Previous | Table of Contents | Next |